$(document).ready(function(){
	var date=new Date;
	var year=date.getFullYear();
	$('#nowyear').val(year);
})

var options = {
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        }
    },
    toolbox: {
    	//显示策略，可选为：true（显示） | false（隐藏），默认值为false  
        show: true, 
        //magicType，动态类型切换，支持直角系下的折线图、柱状图、堆积、平铺转换  
        magicType: {show: true, type: ['line', 'bar']},  
        //restore，还原，复位原始图表  
        restore: {show: true}, 
    },	
    legend: {
        data:['数量']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        data: ['第一季度','第二季度','第三季度','第四季度']
    },
    yAxis: {
    },
    series: [ ]
 };

var viurl = "/statistics/fromtoStatisticsLine.action";
var xlChart = echarts.init(document.getElementById('xldiv'));
xlChart.setOption(options);
load(viurl, xlChart);

/***加载数据***/
function load(url, myChart){
    $.get(mainDomain + url).done(function (data) {
    	var seriesData=[];
    	var datalist=data.list;
    	var strlist = data.strlist;
    	
    	if(datalist.length==0){
    		xlChart.setOption(options, true);
    		// 填入数据
        	xlChart.setOption({
            	legend: {
                    data: data.strlist
                },
                xAxis: {
                    data: data.categories
                },
                yAxis : 
                	[ {
    				name : '箱量(TEU)'
    			}],
                series: [0,0,0,0]
            });
        	return;
    	}
    	
    	var arrayObj = new Array([datalist.length]);
        for(var i=0;i<datalist.length;i++){
        	var jdarr = [0,0,0,0];
			arrayObj[i] = jdarr;
        }
        
        $.each(arrayObj, function(n, item){
        	var replaceindex;
            $.each(datalist, function(i, entry){
            	replaceindex = $.inArray(entry.df,data.categories);
            	if(replaceindex!=-1){
            		arrayObj[i].splice(replaceindex,1,entry.count);
            	}
            }); 
        })
        
        $.each(arrayObj, function(i, entry){
        	seriesData.push({
	            'name':strlist[i],
	            'type':'line',
	            'data':arrayObj[i]
	            });   
        })
        
     // 填入数据
    	xlChart.setOption({
        	legend: {
                data: data.strlist
            },
            xAxis: {
                data: data.categories
            },
            yAxis : 
            	[ {
				name : '箱量(TEU)'
			}],
            series: seriesData
        });
    })
}

function doSearch(){
	var nowyear = $('#nowyear').val();
	var searchurl = viurl;
	if(nowyear!=null&&nowyear!=''){
		searchurl += '?nowyear=' + nowyear;
		load(searchurl, xlChart);
	}else{
		load(viurl, xlChart);
	}
}